package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.IAreaService;
import cn.itcast.bos.utils.BaseAction;
import cn.itcast.bos.utils.PinYin4jUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class AreaAction extends BaseAction<Area>{

	@Autowired
	private IAreaService areaService;

	// 接收文件
	private File file;

	public void setFile(File file) {
		this.file = file;
	}

	@Action(value = "area_batchImport")
	public String batchImport() throws Exception {

		List<Area> list = new ArrayList();
		// 编写解析代码逻辑
		// 基于xsl格式解析
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		// 获取一个表格sheet
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		// 获取行
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null
					|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			list.add(area);
			// 基于pinyin4j 生成城市简码和编码
			String province = area.getProvince().substring(0,
					area.getProvince().length() - 1);
			String city = area.getCity().substring(0,
					area.getCity().length() - 1);
			String district = area.getDistrict().substring(0,
					area.getDistrict().length() - 1);
			// 简码
			String[] headByString = PinYin4jUtils.getHeadByString(province
					+ city + district);
			StringBuffer buffer = new StringBuffer();
			for (String string : headByString) {
				buffer.append(string);
			}
			String shortcode = buffer.toString();
			area.setShortcode(shortcode);
			// 编码
			String citycode = PinYin4jUtils.hanziToPinyin(city);
			area.setCitycode(citycode);
		}
		// 调用业务层,保存数据
		areaService.savebatch(list);
		return null;
	}

	

	// 查询分页数据
	@Action(value = "area_queryPage", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {

		Specification<Area> specification = new Specification<Area>() {
			// Root 用于获取属性字段，CriteriaQuery可以用于简单条件查询，CriteriaBuilder 用于构造复杂条件查询
			@Override
			public Predicate toPredicate(Root<Area> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 创建list集合
				List<Predicate> list = new ArrayList<Predicate>();

				// 简单单表查询
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province")
							.as(String.class), "%" + model.getProvince() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class),
							"%" + model.getCity() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district")
							.as(String.class), "%" + model.getDistrict() + "%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		// 调用业务层,查询数据结果
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Area> pageDate = areaService.findPageDate(specification, pageable);
		pushPageDataToValueStack(pageDate);
		
		return SUCCESS;
	}

}
